﻿using Prism.Commands;
using Quartz;
using System;
using System.Linq;
using System.Threading.Tasks;
using ZYing.QPanel.Helpers;

namespace ZYing.QPanel.ViewModels
{
    public class JobItemViewModel : ViewModelBase
    {
        private readonly JobsViewModel _jobsViewModel;
        public JobItemViewModel(JobKey key, JobsViewModel jobsViewModel)
        {
            this.Key = key;
            this.Group = Key.Group;
            this.Name = Key.Name;
            this._jobsViewModel = jobsViewModel ?? throw new ArgumentOutOfRangeException(nameof(jobsViewModel));
            this.TriggerCommand = new DelegateCommand(this.Trigger);
            this.DeleteCommand = new DelegateCommand(this.Delete);
        }
        public JobKey Key { get; }
        public string Group { get; set; }
        public string Name { get; set; }
        private string _class;

        public string Class
        {
            get { return _class; }
            set { SetProperty(ref _class, value); }
        }

        private DateTimeOffset? _nextFireTime;
        public DateTimeOffset? NextFireTime
        {
            get { return _nextFireTime; }
            set { SetProperty(ref _nextFireTime, value); }
        }
        private Boolean _Concurrent;

        public Boolean Concurrent
        {
            get { return _Concurrent; }
            set { SetProperty(ref _Concurrent, value); }
        }
        private Boolean _Persist;

        public Boolean Persist
        {
            get { return _Persist; }
            set { SetProperty(ref _Persist, value); }
        }
        private bool _Recovery;

        public bool Recovery
        {
            get { return _Recovery; }
            set { SetProperty(ref _Recovery, value); }
        }

        public void Merge(JobItemViewModel other)
        {
            if (other == null) return;
            this.Class = other.Class;
            this.Concurrent = other.Concurrent;
            this.Persist = other.Persist;
            this.Recovery = other.Recovery;
        }
        public void Read(IJobDetail detail)
        {
            this.Class = detail.JobType.FullName;
            this.Concurrent = !detail.ConcurrentExecutionDisallowed;
            this.Persist = detail.PersistJobDataAfterExecution;
            this.Recovery = detail.RequestsRecovery;
        }
        public async Task Refresh(IScheduler scheduler)
        {
            var triggers = await scheduler.GetTriggersOfJob(this.Key);
            if (triggers.Count == 0)
            {
                this.NextFireTime = null;
                return;
            }
            this.NextFireTime = triggers
                .Select(i => i.GetNextFireTimeUtc())
                .Where(i => i != null)
                .OrderBy(i => i)
                .FirstOrDefault();
        }
        public DelegateCommand TriggerCommand { get; }
        public DelegateCommand DeleteCommand { get; }
        public async void Trigger()
        {
            await this._jobsViewModel.Scheduler.TriggerJob(this.Key);
            await this._jobsViewModel.RefreshStatus();
            NotifyHelper.Success("任务触发成功");
        }
        public async void Delete()
        {
            if (MessageHelper.Confirm($"确认要删除任务【{this.Key}】吗？") == false) return;
            await this._jobsViewModel.Scheduler.DeleteJob(this.Key);
            await this._jobsViewModel.Reload();
            NotifyHelper.Success("任务删除成功");
        }
    }
}
